using UnityEngine;
using System.Collections;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class SkyBackground : MonoBehaviour{
	
	public float skyHeight = 1f;
	public float tileWidth = 7f;
	public GameEngine engine;
	
	protected void Awake(){
		
		Rect wb = engine.worldBounds;
		float ew = Mathf.Abs(transform.position.x*2f);
		float uw = ((wb.xMax+ew)-wb.xMin)/tileWidth;
		
		Vector3[] vertices = new Vector3[]{	new Vector3(wb.xMin,wb.yMax+skyHeight,0),
											new Vector3(wb.xMax+ew,wb.yMax+skyHeight,0),
											new Vector3(wb.xMin,wb.yMax,0),
											new Vector3(wb.xMax+ew,wb.yMax,0)};
		int[] triangles = new int[]{0,1,2,1,3,2};
		Vector2[] uv = new Vector2[]{	new Vector2(0,0),
										new Vector2(uw,0),
										new Vector2(0,1),
										new Vector2(uw,1)};
		
		
		Mesh mesh = new Mesh();
		mesh.vertices = vertices;
		mesh.triangles = triangles;
		mesh.uv = uv;
		mesh.RecalculateNormals();
		
		GetComponent<MeshFilter>().mesh = mesh;
	}
	
}